import { observable, action } from 'mobx';
import * as Api from '../../api/CenterApi';

import { Comment,SearchAdvanced } from 'weaver-mobile-page';
import { PARAMS } from '../../util';

export default class RelateStore {
  @observable relatePage = 1;
  @observable collectList = new Comment.Store({ getComment: this.getCollectComment });
  @observable onCollectSearch = new SearchAdvanced.Store({ route: '/center/collect', id: 'collectSearch' }); // 交流高级搜索
  @observable collectAdValue = ''; //高级搜索值受控

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  @action('收藏帖子列表请求')
  getCollectComment = () => {
    const collectSearchParams = this.onCollectSearch.form.formParams || {};
    let defaultparams = {
      currentPage: this.relatePage++,
      ...collectSearchParams,
    }
    return Api.getCoworkCollectList(defaultparams).then(res => res)
  }

  @action('收藏的帖子高级搜索')
  getCoworkCollectShareCondition = (params) => {
    this.onCollectSearch.getData({
      url:`/api/cowork/collect/getCoworkCollectShareCondition`,
      method: `GET`,
      params: {...params, ...PARAMS}
    })
  }

  @action('协作-协作交流-置顶/取消置顶/审批/收藏/删除评论/赞 交流记录')
  coworkDiscussOperation = (data, parent, val, callback) => {
    let params = {
      id: data.replyId,
      coworkId: data.coworkId,
      operationType: val,
      discussId: parent ? parent.replyId : '',
      menuType :"myCollect",
    }
    return Api.coworkDiscussOperation(params).then(res => {
      if(val === 'collect'){
        this.relatePage = 1;
        this.collectList.getDatas();  // 刷新列表重新渲染数据
      }else{
        this.collectList.doOperateReply({
          replyId: data.replyId,
          parentId: parent ? parent.replyId : '',
          reply: res.replyList[0]
        })
      }
      
     
      if (callback) callback();
    })
  }

  @action('搜索操作')
  collectSearch = () => {
    this.relatePage = 1;
    this.collectList.getDatas();  // 刷新列表重新渲染数据
  }

  // 我的收藏 -- 高级搜素 -- 重置
  @action doCollectReset = () => {
    this.onCollectSearch.form.resetFields();
    this.collectAdValue = '';
    this.getCollectComment();
  }

  // 我的收藏 -- 高级搜索内外name统一
  @action updateCollectAdvance = (value) => {
    this.collectAdValue = value;
    this.onCollectSearch.form.updateField('remark', value);
  }

  // 组件卸载执行操作
  clear = () => {
    this.onCollectSearch.form.resetFields();  // 重置高级列表数据
    this.collectAdValue = '';
    this.relatePage = 1;
  }
} 